/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import auxx.FileManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author yan
 */
public class LooserFrame extends JFrame{

    
    
    JButton changename,viewstats;
    JLabel name,score;
    Box main,stats,namebox,scorebox;
    BallGame bg;
    Eventer evt;
    
    public LooserFrame () {
        init();
    }

    private void init() {
        bg = BallGame.BG;
        evt =  new Eventer();
        
        changename = new JButton("Change");
        changename.addActionListener(evt);
        viewstats = new JButton("View Stats");
        
        main = Box.createVerticalBox();
        stats = Box.createVerticalBox();
        namebox = Box.createHorizontalBox();
        scorebox = Box.createHorizontalBox();
        
        name = new JLabel(bg.name);
        score = new JLabel(bg.score + "");
        
        namebox.add(new JLabel("Name:"));
        namebox.add(Box.createHorizontalStrut(40));
        namebox.add(Box.createHorizontalGlue());
        namebox.add(name);
        namebox.add(Box.createHorizontalStrut(5));
        namebox.add(changename);
        
        scorebox.add(new JLabel("Score:"));
        scorebox.add(Box.createHorizontalStrut(15));
        scorebox.add(Box.createHorizontalGlue());
        scorebox.add(score);
        scorebox.add(Box.createHorizontalStrut(50));
        
        main.add(namebox);
        main.add(Box.createVerticalStrut(10));
        main.add(scorebox);
        
        add(main);
        addWindowListener(evt);
        pack();
        setLocationRelativeTo(bg.f);
//        setVisible(true);
    }
    
    
    private class Eventer extends WindowAdapter implements ActionListener{

        @Override
        public void windowClosing(WindowEvent e) {
            if(bg.d!=Difficulty.CUSTOM)
                bg.writeScore();
            System.exit(0);
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==changename) {
                String aux = JOptionPane.showInputDialog(null, "Enter your name:","Can't you decide?",JOptionPane.QUESTION_MESSAGE);
                if(aux!=null) bg.name = aux;
                FileManager.delete(new File("config"));
                bg.checkFiles();
                name.setText(bg.name);
                System.out.println(bg.name);
                pack();
            }
        }
    }
    
}